package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.fc.cp.api.ViewCusersQueryCmd;
import com.jackrain.nea.fc.cp.mapper.ViewCusersMapper;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 用户档案服务接口service层
 *
 * @author wuwenxue
 * @date 2017/11/07
 **/
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "cp")
public class ViewCusersQueryCmdImpl extends CommandAdapter implements ViewCusersQueryCmd {
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        ViewCusersMapper viewCusersMapper = ApplicationContextHandle.getBean(ViewCusersMapper.class);
        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        log.info("ViewCusersQueryCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        Object[] id = param.getJSONArray("IDS").toArray();
        JSONArray jsonArray = new JSONArray();

        List<HashMap> list = viewCusersMapper.findAllUsers(id);
        for (HashMap<String, Object> hashMap : list) {
            //外键关联：店仓
            Long storeID = TypeUtils.castToLong(hashMap.get("CP_C_STORE_ID"));
            if (null != storeID) {
                String ecode = viewCusersMapper.findByStoreID(storeID);
                hashMap.put("CP_C_STORE_ID", ecode);
            }

            //外键关联：配销中心
            Long distribID = TypeUtils.castToLong(hashMap.get("CP_C_DISTRIB_ID"));
            if (null != distribID) {
                String ecode = viewCusersMapper.findByDistribID(distribID);
                hashMap.put("CP_C_DISTRIB_ID", ecode);
            }

            //查询用户表USERS MODIFIERID
            Long modifierId = TypeUtils.castToLong(hashMap.get("MODIFIERID"));
            if (null != modifierId) {
                String ename = viewCusersMapper.findByUserId(modifierId);
                hashMap.put("MODIFIERID", ename);
            }

//            String yes = "Y";
//            String no = "N";
//            String isactive = TypeUtils.castToString(hashMap.get("ISACTIVE"));
//            if (yes.equals(isactive)) {
//                hashMap.put("ISACTIVE", 1);
//            } else if (no.equals(isactive)) {
//                hashMap.put("ISACTIVE", 2);
//            }

            //部门
            Long deptName = TypeUtils.castToLong(hashMap.get("CP_C_HRORG_ID"));
            if (null != deptName) {
                String ecode = viewCusersMapper.findByOrg(deptName);
                hashMap.put("CP_C_HRORG_ID", ecode);
            }

            String sex = TypeUtils.castToString(hashMap.get("SEX"));
            String man = "M";
            if (null != sex) {
                if (man.equals(sex)) {
                    hashMap.put("SEX", 1);
                } else {
                    hashMap.put("SEX", 2);
                }
            }
            log.info(TypeUtils.castToString(hashMap.get("NAME")));
            JSONObject js = new JSONObject(hashMap);
            JSONObject param1 = JSON.parseObject(JSON.toJSONStringWithDateFormat(js, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param1);

        }
        vh.put("data", jsonArray);
        return vh;
    }
}
